<template>
  <!-- <el-dialog v-model="visible" title="打印配置" width="500"> -->
  <!-- <el-form :model="form" ref="formRef" labelWidth="100px">
    <el-form-item
      label="打印内容"
      prop="type"
      :rules="[
        {
          required: true,
          message: '请选择打印内容',
          trigger: 'blur',
        },
      ]"
    >
      <el-radio-group v-model="form.type">
        <el-radio :value="0" name="type"> 二维码+编码 </el-radio>
        <el-radio :value="1" name="type"> 编码 </el-radio>
      </el-radio-group>
    </el-form-item>
  </el-form> -->

  <!-- <div class="dialog-footer">
    <el-button type="primary" @click="print" :loading="loading">
      确定
    </el-button>
    <el-button @click="onCancel">取消</el-button>
  </div> -->

  <!-- <DialogEasyPrint ref="dialogEasyPrintRef"></DialogEasyPrint> -->
  <!-- </el-dialog> -->
</template>
<script setup>
import { ref } from "vue";
import { ElMessage } from "element-plus";
// import api from "@/api/label.js";
import { downloadFn } from "@/api/index.js";
// import DialogEasyPrint from "@/components/dialogEasyPrint.vue";

const props = defineProps({
  url: { type: String, default: "/admin-api/system/label/print" },
  ids: { type: Array, default: () => [] },
  labelCodes: { type: Array, default: () => [] },
  type: { type: [String, Number], default: "" },
  batchno: { type: [String, Number], default: "" },
});
// const dialogEasyPrintRef = ref();
const visible = ref(false);
const loading = ref(false);
const formRef = ref();
// const form = ref({
//   type: 0,
// });

const doPrint = (val) => {
  const ordonnance = document.getElementById(val).contentWindow;
  setTimeout(() => {
    ordonnance.print();
    loading.value = false;
  }, 100);
};

const printBtnClick = (record) => {
  const blob = new Blob([record], { type: "application/pdf;charset=utf-8" });
  var date = new Date().getTime();
  var ifr = document.createElement("iframe");
  ifr.style.frameborder = "no";
  ifr.style.display = "none";
  ifr.style.pageBreakBefore = "always";
  ifr.setAttribute("id", "printPdf" + date);
  ifr.setAttribute("name", "printPdf" + date);
  ifr.src = window.URL.createObjectURL(blob);
  document.body.appendChild(ifr);
  doPrint("printPdf" + date);
  window.URL.revokeObjectURL(ifr.src); // 释放URL 对象
};

const print = async (params) => {
  loading.value = true;
  try {
    const res = await downloadFn(props.url, params);
    if (res) {
      printBtnClick(res);
    }
  } finally {
    loading.value = false;
  }
  return true;
};

const onCancel = () => {
  formRef.value.clearValidate();
  visible.value = false;
};

defineExpose({
  print,
  onCancel,
});
</script>
